//
//  CRDialogueFrame.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/19.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRDialogueFrame.h"
@implementation CRDialogueFrame

- (void)setDialogue:(CRDialogue *)dialogue
{
    _dialogue = dialogue;
    CGSize selfSize = [UIScreen mainScreen].bounds.size;
    
    CGFloat iconX = DiconX;
    CGFloat iconY = DiconY;
    CGFloat iconW = DiconW;
    CGFloat iconH = DiconH;

    //头像frame
    //如果是发送者
    if (dialogue.dialogueType == sendDType) {
        iconX = selfSize.width - iconW - Dmargin;
    }
    self.iconRect = CGRectMake(iconX, iconY, iconW, iconH);
    
    //时间
    if (!self.showTime) {
        CGFloat timeY = DTimeY;
        CGSize timeSize = [self.dialogue.currentTime sizeWithAttributes:@{UIFontDescriptorSizeAttribute:@"11"}];
        CGFloat timeX = (selfSize.width - timeSize.width) / 2;
        self.currentTimeRect = CGRectMake(timeX, timeY, timeSize.width + Dmargin, timeSize.height + Dmargin);
    }
   
    //文本内容
    CGFloat textX = CGRectGetMaxX(self.iconRect) ;
    CGFloat textY = iconY;
    CGSize textSize = [self sizeWithText:dialogue.content andMaxSize:CGSizeMake(250, MAXFLOAT) andFont:DcontentText];
   
    //如果是发送者发送内容
    if (dialogue.dialogueType == sendDType) {
        textX = selfSize.width - iconW - Dmargin * 5  - textSize.width;
    }
    if (dialogue == nil) {
        
    }
//    self.pictureRect = CGRectMake(textX + textSize.width, iconY, DpictureW, DpictureH);
    //如果有图片
    if (dialogue.picture) {
        
        self.contentRect = CGRectMake(textX, textY, textSize.width + Dmargin * 4 + DpictureW, textSize.height + Dmargin * 2 + DpictureH);
    }else{
        self.contentRect = CGRectMake(textX, textY, textSize.width + Dmargin * 4 , textSize.height + Dmargin * 2);
    }
    
    //返回cell高 MAX(A,B)取两者CGRectGetMaxY（最大Y值）中最大的
    self.cellHeigth = MAX(CGRectGetMaxY(self.iconRect), CGRectGetMaxY(self.contentRect)) + Dmargin;
    
    
}
//计算文本size
- (CGSize)sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
{
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
}





@end
